<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
function isMobile()
{
    // 如果有HTTP_X_WAP_PROFILE则一定是移动设备
    if (isset ($_SERVER['HTTP_X_WAP_PROFILE']))
    {
        return true;
    }
    // 如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
    if (isset ($_SERVER['HTTP_VIA']))
    {
        // 找不到为flase,否则为true
        return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false;
    }
    // 判断手机发送的客户端标志,兼容性有待提高
    if (isset ($_SERVER['HTTP_USER_AGENT']))
    {
        $clientkeywords = array ('nokia',
            'sony',
            'ericsson',
            'mot',
            'samsung',
            'htc',
            'sgh',
            'lg',
            'sharp',
            'sie-',
            'philips',
            'panasonic',
            'alcatel',
            'lenovo',
            'iphone',
            'ipod',
            'blackberry',
            'meizu',
            'android',
            'netfront',
            'symbian',
            'ucweb',
            'windowsce',
            'palm',
            'operamini',
            'operamobi',
            'openwave',
            'nexusone',
            'cldc',
            'midp',
            'wap',
            'mobile'
        );
        // 从HTTP_USER_AGENT中查找手机浏览器的关键字
        if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT'])))
        {
            return true;
        }
    }
    return false;
}

/**
 * 获取全局唯一标识符
 * @param bool $trim
 * @return string
 */
function getGuidV4($trim = true)
{
    // Windows
    if (function_exists('com_create_guid') === true) {
        $charid = com_create_guid();
        return $trim == true ? trim($charid, '{}') : $charid;
    }
    // OSX/Linux
    if (function_exists('openssl_random_pseudo_bytes') === true) {
        $data = openssl_random_pseudo_bytes(16);
        $data[6] = chr(ord($data[6]) & 0x0f | 0x40);    // set version to 0100
        $data[8] = chr(ord($data[8]) & 0x3f | 0x80);    // set bits 6-7 to 10
        return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
    }
    // Fallback (PHP 4.2+)
    mt_srand((double)microtime() * 10000);
    $charid = strtolower(md5(uniqid(rand(), true)));
    $hyphen = chr(45);                  // "-"
    $lbrace = $trim ? "" : chr(123);    // "{"
    $rbrace = $trim ? "" : chr(125);    // "}"
    $guidv4 = $lbrace .
        substr($charid, 0, 8) . $hyphen .
        substr($charid, 8, 4) . $hyphen .
        substr($charid, 12, 4) . $hyphen .
        substr($charid, 16, 4) . $hyphen .
        substr($charid, 20, 12) .
        $rbrace;
    return $guidv4;
}

/**
 * json数据失败code
 */
const JSON_STATUS_FAIL = 0;

/**
 * json数据成功code
 */
const JSON_STATUS_SUCCESS = 1;

/**
 * 返回成功数据
 * @param array $data
 * @param int $code
 * @param string $msg
 */
function jsonSuccess($data = '', $code = JSON_STATUS_SUCCESS, $msg = 'success')
{
    if (empty($data)) {
        $result = [
            'code' => $code,
            'msg' => $msg,
        ];
    } else {
        $result = [
            'code' => $code,
            'msg' => $msg,
            'data' => $data,
        ];
    }
    header('Content-Type:application/json; charset=utf-8');
    http_response_code(200);
    exit(json_encode($result));
}

/**
 * 返回失败数据
 * @param string $msg
 * @param int $code
 * @param array $data
 */
function jsonFail($msg = 'fail', $code = JSON_STATUS_FAIL)
{
    $result = [
        'code' => 0,
        'msg' => $msg,
    ];
    if ($code === -1) {
        http_response_code(401);
    }
    header('Content-Type:application/json; charset=utf-8');
    exit(json_encode($result));
}

/**
 * 判断是否在微信浏览器
 * @return bool
 */
function is_weixin(){
    if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false ) {
        return true;
    }
    return false;
}

/**
 * curl请求指定url
 * @param $url
 * @param array $data
 * @return mixed
 */
function curl($url, $data = [])
{
    // 处理get数据
    if (!empty($data)) {
        $url = $url . '?' . http_build_query($data);
    }
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    $result = curl_exec($curl);
    curl_close($curl);
    return $result;
}

// 获取装修风格
function getStyle($style)
{
    //$val = explode(',', $style)[0];
    switch ($style){
        case 1:
            $name = "现代";
            break;
        case 2:
            $name = "简约";
            break;
        case 3:
            $name = "北欧";
            break;
        case 4:
            $name = "美式";
            break;
        case 5:
            $name = "轻奢";
            break;
        case 6:
            $name = "日式";
            break;
        case 7:
            $name = "混搭";
            break;
        case 8:
            $name = "新中式";
            break;
        case 9:
            $name = "轻工业";
            break;
        case 10:
            $name = "其它";
            break;
        default:
            $name = "其它";
    }
    return $name;
}

/**
 * 获取设计师类型
 * @param $type
 * @return string
 */
function getDesignerType($type)
{
    //$val = explode(',', $style)[0];
    switch ($type){
        case 1:
            $name = "主案设计师";
            break;
        case 2:
            $name = "美居规划师";
            break;
        case 3:
            $name = "改造师";
            break;
        case 4:
            $name = "定制设计师";
            break;
        default:
            $name = "其它";
    }
    return $name;
}

/**
 * 获取软装百科分类名称
 * @param $type
 * @return string
 */
function getBaikeName($type)
{
    switch ($type){
        case 21:
            $name = "软装搭配";
            break;
        case 22:
            $name = "全屋定制";
            break;
        case 23:
            $name = "精装改造";
            break;
        case 24:
            $name = "软装知识";
            break;
        default:
            $name = "软装搭配";
    }
    return $name;
}

/**
 * 获取软装百科分类编码
 * @param $type
 * @return string
 */
function getBaikeCode($type)
{
    switch ($type){
        case 21:
            $name = "rzdp";
            break;
        case 22:
            $name = "qwdz";
            break;
        case 23:
            $name = "jzgz";
            break;
        case 24:
            $name = "rzzs";
            break;
        default:
            $name = "rzdp";
    }
    return $name;
}